
** 

use health/demographics , clear


gen ever_test = nTests > 0 
assert !missing(nTests)
gen positivity = posTest if ever_test

gen c0 = "Sample" in 1
local c=  0 
qui foreach sample in in_test in_inp inp_has_dx ever_not_icli ever_clear ever_icli {
		
	local row = 1
	local ++c
	if "`sample'" == "in_test" gen c`c' = "Test &" in `row'
	if "`sample'" == "in_inp" gen c`c' = "Inpatient &" in `row'
	if "`sample'" == "inp_has_dx" gen c`c' = "Inpatient, has DX &" in `row'
	if "`sample'" == "ever_icli" gen c`c' = "Inpatient, ever ICLI &" in `row'
	if "`sample'" == "ever_clear" gen c`c' = "Ever clear cause &" in `row'
	if "`sample'" == "ever_not_icli" gen c`c' = "Inpatient, ever not ILCI &" in `row'
	
	local ++row 
	replace c0 = "Age as of 1/1 \\" in `row'		
	foreach a of numlist 0 17 29 49 64 74 75 {
		
		if `a' == 0  local vn 	"  Born after 1/1"
		if `a' == 17 local vn 	"  0-17"
		if `a' == 29 local vn 	"  18-29"
		if `a' == 49 local vn 	"  30-50"
		if `a' == 64 local vn 	"  50-64"
		if `a' == 74 local vn 	"  65-74"
		if `a' == 75 local vn 	"  75+"
		
		local ++row
		replace c0 = "`vn' &" in `row'
		sum `a'.age_wide  if `sample'
		replace c`c' = string(r(mean),"%4.3f") + " & " in `row'
	}
	local ++row
	count if `sample' == 1
	local nn = r(N)
	count if missing(age) & `sample' ==1 
	replace c0 = "Age unknown &" in `row'
	replace c`c' = string(r(N)/`nn', "%4.3f") + " & " in `row'
	
	
	local ++row
	replace c0 = "\\" in `row'
	local ++row
	replace c0 = "Gender \\"  in `row'
	foreach v of varlist male female {
		
		local ++row
		replace c0 = proper("`v'") + " & " in `row'
		sum `v' if `sample' & gender~="U"
		replace c`c' = string(r(mean), "%4.3f") + " & " in `row'			
		
	}
	local ++row
	sum gender_unknown if `sample' 
	local ++row
	replace c0 = "Unknown  & " in `row'
	replace c`c' = string(r(mean), "%4.3f") + " & " in `row'		

	
	local ++row
	replace c0 = "\\" in `row'
	local ++row
	replace c0 = "Race/ethnicity \\" in `row'
	foreach v in white black raceUnknown  {
			
		if "`v'" == "white" local vn "White"
		if "`v'" == "black" local vn "Black"
		if "`v'" == "raceUnknown" local vn "Race unknown"
		
		local known "~raceUnknown"
		
		local ++row
		replace c0 = "`vn' & " in `row'
		sum `v' if `sample' & `known' 
		replace c`c' = string(r(mean),"%4.3f") + " & " in `row'
		
	}
	local ++row
	replace c0 = "\\" in `row'
	local ++row
	replace c0 = "Test variables \\" in `row'
	foreach v in ever_test posTest   {
		
		if "`v'" == "has_zip" local vn Has ZIP code
		if "`v'" == "has_cb" local vn Has census block
		if "`v'" == "died" local vn Died
		if "`v'" == "ever_test" local vn Ever tested  
		if "`v'" == "posTest" local vn Confirmed positive
		
		local ++row
		replace c0 = "`vn' &" in `row'
		sum `v' if `sample'
		replace c`c' = string(r(mean), "%4.3f") + " & " in `row'
	}
	
	
	local ++row
	replace c0 = "\\" in `row'
	local ++row
	replace c0 = "People &" in `row'
	count if `sample'
	replace c`c' =string(r(N),"%12.0fc") + " & " in `row'

	egen tag_sample_county = tag(`sample' county)
	count if tag_sample_county & `sample'
	local ++row
	replace c0 = "Counties &" in `row'
	replace c`c' = string(r(N), "%3.0f") + " & " in `row'
	drop tag_sample_county
	
}

** manually add census info
gen cIN = " &" 
foreach n in 2 11 12 15 17 18 22 23 26 {
	replace cIN = " " in `n'
}
* age distribution
replace cIN = "0.237 &" in 4
replace cIN = "0.166 &" in 5
replace cIN = "0.250 &" in 6
replace cIN = "0.197 &" in 7
replace cIN = "0.087 &" in 8
replace cIN = "0.063 &" in 9

* male/female
replace cIN = "0.493 &" in 13
replace cIN = "0.507 &" in 14

* white/black
replace cIN = "0.848 &" in 19
replace cIN = "0.099 &" in 20

** ever tested/positive
count if ever_test==1
replace cIN = string(r(N)/6637426, "%4.3f") + " & " in 24
count if posTest==1
replace cIN = string(r(N)/6637426, "%4.3f") + " & " in 25

** counts
replace cIN = "6,637,426 &" in 27
replace cIN = "92 &" in 28

list c0 cIN in 1/28, clean

replace c6 = subinstr(c6, "&", "\\", .) 
list c0 cIN c1-c6 in 1/`row', noobs clean
outsheet c0 cIN c1-c6 in 2/`row' using tables/demographics_by_test_and_hospital.tex, ///
	replace noquote nonames delimit(" ")
	
